using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class InvoiceRegisterControl
    {
        #region Local Variable
        private InvoiceRegisterDataAccess _InvoiceRegisterDAO;
        #endregion Local Variable

        #region Method
        public InvoiceRegisterControl()
        {
            _InvoiceRegisterDAO = new InvoiceRegisterDataAccess();
        }

        public object BeginTransaction(ref string sErr)
        {
            return _InvoiceRegisterDAO.BeginTransaction(ref sErr);
        }
        public bool RollbackTransaction(ref string sErr)
        {
            return _InvoiceRegisterDAO.RollbackTransaction(ref sErr);
        }
        public bool CommitTransaction(ref string sErr)
        {
            return _InvoiceRegisterDAO.CommitTransaction(ref sErr);
        }
        //public InvoiceRegisterInfo Get_InvoiceRegister(
        //    String InvNo,
        //    String Creditor,
        //    Int16 LineNo
        //, ref string sErr)
        //{
        //    return _InvoiceRegisterDAO.Get_InvoiceRegister(
        //    InvNo,
        //    Creditor,
        //    LineNo
        //    , ref sErr);
        //}

        //public InvoiceRegisterInfo Get_InvoiceRegister1(
        //    String InvNo,
        //    String Creditor,
        //    Int16 LineNo
        //, ref string sErr)
        //{
        //    return _InvoiceRegisterDAO.Get_InvoiceRegister1(
        //    InvNo,
        //    Creditor,
        //    LineNo
        //    , ref sErr);
        //}

        public DataTable GetCond(string budId, String strCond, ref string sErr)
        {
            if (budId == "HL") return _InvoiceRegisterDAO.GetCondHL(strCond, ref sErr);
            return _InvoiceRegisterDAO.GetCondHV(strCond, ref sErr);
        }

        public DataTable GetAll_InvoiceRegister(ref string sErr)
        {
            return _InvoiceRegisterDAO.GetAll_InvoiceRegister(ref sErr);
        }

        public Boolean IsExist_InvoiceRegisterOurRef(
          String OurRef
      )
        {
            return _InvoiceRegisterDAO.IsExist_InvoiceRegisterOurRef(
            OurRef
            );
        }
        public Boolean IsExist_InvoiceRegisterOurRef1(
            String OurRef
        )
        {
            return _InvoiceRegisterDAO.IsExist_InvoiceRegisterOurRef1(
            OurRef
            );
        }

        public DataTable GetAll_InvoiceRegister1(ref string sErr)
        {
            return _InvoiceRegisterDAO.GetAll_InvoiceRegister1(ref sErr);
        }

        public Int32 Add_InvoiceRegister(InvoiceRegisterInfo obj, ref string sErr)
        {
            return _InvoiceRegisterDAO.Add_InvoiceRegister(obj, ref sErr);
        }
        public Int32 Add_InvoiceRegister1(InvoiceRegisterInfo obj, ref string sErr)
        {
            return _InvoiceRegisterDAO.Add_InvoiceRegister1(obj, ref sErr);
        }

        public string Update_InvoiceRegister(InvoiceRegisterInfo obj)
        {
            return _InvoiceRegisterDAO.Update_InvoiceRegister(obj);
        }
        public string Update_InvoiceRegister1(InvoiceRegisterInfo obj)
        {
            return _InvoiceRegisterDAO.Update_InvoiceRegister1(obj);
        }

        public string Update_Transmit(string busId, InvoiceRegisterInfo obj)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.Update_TransmitHL(obj);
            return _InvoiceRegisterDAO.Update_TransmitHV(obj);
        }
        public string Update_Return(string busId, InvoiceRegisterInfo obj)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.Update_ReturnHL(obj);
            return _InvoiceRegisterDAO.Update_ReturnHV(obj);
        }

        //public string Delete_InvoiceRegister(
        //    String InvNo,
        //    String Creditor,
        //    Int16 LineNo
        //)
        //{
        //    return _InvoiceRegisterDAO.Delete_InvoiceRegister(
        //    InvNo,
        //    Creditor,
        //    LineNo
        //    );
        //}
        // public string Delete_InvoiceRegister1(
        //    String InvNo,
        //    String Creditor,
        //    Int16 LineNo
        //)
        // {
        //     return _InvoiceRegisterDAO.Delete_InvoiceRegister1(
        //     InvNo,
        //     Creditor,
        //     LineNo
        //     );
        // }

        public Boolean IsExist_InvoiceRegister(String InvNo, String VendorCode = null, Int16 LineNo = -1)
        {
            return _InvoiceRegisterDAO.IsExist_InvoiceRegister(
            InvNo,
            VendorCode,
            LineNo
            );
        }

        public Boolean IsExist_InvoiceRegister1(String InvNo, String VendorCode = null, Int16 LineNo = -1)
        {
            return _InvoiceRegisterDAO.IsExist_InvoiceRegister1(
            InvNo,
            VendorCode,
            LineNo
            );
        }

        /*  public string Delete_InvoiceRegister(String arrID)
          {
              string kq = "";
              string[] arrStrID = arrID.Split(',');
              foreach (string strID in arrStrID)
              {
                  if (strID != "")
                  {
                      int ID = Convert.ToInt32(strID);
                      kq += _InvoiceRegisterDAO.Delete_InvoiceRegister(ID);
                  }
              }
              return kq;
          } */

        //public DataTableCollection Get_Page(InvoiceRegisterInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        //{
        //    return _InvoiceRegisterDAO.Get_Page(obj, orderBy, pageIndex, pageSize, ref sErr);
        //}

        //public DataTable Search(String columnName, String columnValue, String condition, ref String sErr)
        //{
        //    return _InvoiceRegisterDAO.Search(columnName, columnValue, condition, ref  sErr);
        //}

        #endregion Method


        public string getNextRef(ref string sErr, string BusId)
        {
            if (BusId == "HL") return getNextRefHL(ref sErr);
            return getNextRefHV(ref sErr);
        }

        private string getNextRefHV(ref string sErr)
        {
            return _InvoiceRegisterDAO.getNextRefHV(ref  sErr);
        }

        private string getNextRefHL(ref string sErr)
        {
            return _InvoiceRegisterDAO.getNextRefHL(ref  sErr);

        }

        public bool IsExistInSun(string ourRef, string connectString, string db)
        {
            return _InvoiceRegisterDAO.IsExistInSun(db, ourRef, connectString);
        }

        public bool IsExistInExpenseClaim(string BusID, string ourRef)
        {
            return _InvoiceRegisterDAO.IsExistInExpenseClaim(BusID, ourRef);
        }

        public bool IsValidPeriod(int p)
        {
            return _InvoiceRegisterDAO.IsValidPeriod(p);
        }

        public decimal getConvRate(string busid, DateTime cur, ref string sErr)
        {
            if (busid == "HL") return _InvoiceRegisterDAO.getConvRateHL(cur, ref sErr);
            return _InvoiceRegisterDAO.getConvRateHV(cur, ref sErr);
        }

        public decimal getTotalAmt(string busId, ref string sErr, int reference = -1, int lineno = -1)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getTotalAmtHL(ref sErr, reference, lineno);
            return _InvoiceRegisterDAO.getTotalAmtHV(ref sErr, reference, lineno);
        }

        public DataTable getTotalAmts(string busId, int reference, ref string sErr)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getTotalAmtsHL(ref sErr, reference);
            return _InvoiceRegisterDAO.getTotalAmtsHV(ref sErr, reference);
        }

        public DataTable getTransmitRef(string busId, ref string sErr)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getTransmitRefHL(ref sErr);
            return _InvoiceRegisterDAO.getTransmitRefHV(ref sErr);
        }

        public DataTable getReturnRef(string busId, ref string sErr)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getReturnRefHL(ref sErr);
            return _InvoiceRegisterDAO.getReturnRefHV(ref sErr);
        }

        public string UpdateField(string busId, string name, object value, InvoiceRegisterInfo inf)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.updateFieldHL(name, value, inf);
            return _InvoiceRegisterDAO.UpdateFieldHV(name, value, inf);
        }

        public int getMaxLineNo(string busId, int refer, ref string sErr)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getMaxLineNoHL(refer, ref  sErr);
            return _InvoiceRegisterDAO.getMaxLineNoHV(refer, ref  sErr);
        }

        public decimal getContractTotalAmt(string busId, string code, ref string sErr, int refer = -1, int line = -1)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getContractTotalAmtHL(code, ref sErr, refer, line);
            return _InvoiceRegisterDAO.getContractTotalAmtHV(code, ref sErr, refer, line);
        }

        public decimal getBudgetTotalAmt(string busId, string code, int year, ref string sErr, int refer = -1, int line = -1)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.getBudgetTotalAmtHL(code, year, ref sErr, refer, line);
            return _InvoiceRegisterDAO.getBudgetTotalAmtHV(code, year, ref sErr, refer, line);
        }


        public string Delete(string busId, int refer, short lineNo)
        {
            if (busId == "HL") return _InvoiceRegisterDAO.DeleteHL(refer, lineNo);
            return _InvoiceRegisterDAO.DeleteHV(refer, lineNo);
        }

        public bool IsExistOurRef(string BusID, string ourRef)
        {
            if (BusID == "HL") return _InvoiceRegisterDAO.IsExist_InvoiceRegisterOurRef(ourRef);
            else return _InvoiceRegisterDAO.IsExist_InvoiceRegisterOurRef1(ourRef);
        }

        public int getMaxTransmitRef(string BusID, int year, int month, ref string sErr)
        {
            int maxRef = 0;

            if (BusID == "HL") maxRef = _InvoiceRegisterDAO.getMaxTransmitRefHL(year, month, ref  sErr);
            else maxRef = _InvoiceRegisterDAO.getMaxTransmitRefHV(year, month, ref  sErr);

            if (maxRef == 0) maxRef = ((year % 100) * 100 + month) * 1000;
            else if (maxRef / 1000 != ((year % 100) * 100 + month)) maxRef = ((year % 100) * 100 + month) * 1000;
            return maxRef;
        }

        public int getMaxReturnRef(string BusID, int year, int month, ref string sErr)
        {
            int maxRef = 0;

            if (BusID == "HL") maxRef = _InvoiceRegisterDAO.getMaxReturnRefHL(year, month, ref  sErr);
            else maxRef = _InvoiceRegisterDAO.getMaxReturnRefHV(year, month, ref  sErr);

            if (maxRef == 0) maxRef = ((year % 100) * 100 + month) * 1000;
            else if (maxRef / 1000 != ((year % 100) * 100 + month)) maxRef = ((year % 100) * 100 + month) * 1000;
            return maxRef;
        }

        public int add(string BusId, InvoiceRegisterInfo inf, ref string sErr)
        {
            if (BusId == "HL") return Add_InvoiceRegister(inf, ref sErr);
            return Add_InvoiceRegister1(inf, ref sErr);
        }

        public string update(string BusId, InvoiceRegisterInfo info)
        {
            if (BusId == "HL") return Update_InvoiceRegister(info);
            return Update_InvoiceRegister1(info);
        }

        public bool IsOurRefModified(string BusId, int refer, int line, string ourRef)
        {
            if (BusId == "HL") return _InvoiceRegisterDAO.IsOurRefModified(refer, line, ourRef);
            return _InvoiceRegisterDAO.IsOurRefModified1(refer, line, ourRef);
        }

        public bool IsExist(int refer, short line)
        {
            return _InvoiceRegisterDAO.IsExist(refer, line);
        }

        public bool IsExist1(int refer, short line)
        {
            return _InvoiceRegisterDAO.IsExist1(refer, line);
        }

        public string GetCreator(string BusId, string value)
        {
            if (BusId == "HL") return _InvoiceRegisterDAO.GetCreatorHL(value);
            return _InvoiceRegisterDAO.GetCreatorHV(value);
        }


        public bool IsExistInvoiceNo(string invNo, int refer, string creditor)
        {
            return _InvoiceRegisterDAO.IsExistInvoiceNoHL(invNo, refer, creditor);
        }

        public bool IsExistInvoiceNo1(string invNo, int refer, string creditor)
        {
            return _InvoiceRegisterDAO.IsExistInvoiceNoHV(invNo, refer, creditor);
        }
    }
}
